package user

import (
	"context"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUsersDao(ctx context.Context, svcCtx *svc.ServiceContext) *UserDao {
	return &UserDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetOneUser -根据条件查询单个用户
func (l *UserDao) GetOneUser(conditions []predicate.HzUser) (*ent.HzUser, error) {
	userInfo, err := l.svcCtx.Orm.HzUser.Query().Where(conditions...).First(l.ctx)
	if err != nil {
		return nil, err
	}
	return userInfo, nil
}

// CreateUser -创建用户
func (l *UserDao) CreateUser(userPhone, ipAddress string, tx *ent.Tx) (*ent.HzUser, error) {
	userInfo, err := tx.HzUser.Create().
		SetUserPhone(userPhone).
		SetUserRegisterIPAddress(ipAddress).
		SetCreatedAt(time.Now().Unix()).
		SetUpdateAt(time.Now().Unix()).
		Save(l.ctx)
	if err != nil {
		return nil, err
	}
	return userInfo, nil
}
